package com.example.vacateapplication.picker;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.example.vacateapplication.R;
import com.example.vacateapplication.eventbus.PickerMessageEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class PickerUntil {

    private OptionsPickerView leaveTypeOptions ,iKnowOptions ,needKnowOptions;

    private final int MESSAGE_TYPE_LEAVE = 1;//请假类型
    private final int MESSAGE_I_KNOW = 2;//我已知
    private final int MESSAGE_NEED_KNOW = 3;//需知
    private String checkedLeaveType;
    private String checkedIKnow;
    private String checkedNeedKnow;
    private static Context context;
    boolean isChanged = false;//判断是否改变选择
    public List<String> leaveTypeList = new ArrayList<>();//事假类型
    public List<String> iKnowList = new ArrayList<>();//我已知
    public List<String> needKnowList = new ArrayList<>();//需知
    public PickerUntil(Context pContext){
        context = pContext;
        initLeaveTypeData();
        iKnowData();
        needKnowData();
    }


    //请假类型 选择器
    public void showLeaveTypePicker(){
        isChanged = false;
        leaveTypeOptions = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                checkedLeaveType = leaveTypeList.get(options1);
            }
        }).setLayoutRes(R.layout.custom_leave_type, new CustomListener() {
            @Override
            public void customLayout(View v) {
                final TextView tvSubmit = v.findViewById(R.id.picker_job_inc_bg).findViewById(R.id.include_picker_tv_submit);
                final TextView tvCancle = v.findViewById(R.id.picker_job_inc_bg).findViewById(R.id.include_picker_tv_cancle);
                tvSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(leaveTypeList.size()==0){
                            return;
                        }
                        if(!isChanged){//没有改变默认选择第一项
                            EventBus.getDefault().post(new PickerMessageEvent(MESSAGE_TYPE_LEAVE,leaveTypeList.get(0),"0"));
                        }else {
                            EventBus.getDefault().post(new PickerMessageEvent(MESSAGE_TYPE_LEAVE,checkedLeaveType,"0"));
                        }
                        leaveTypeOptions.dismiss();
                    }
                });
                tvCancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(leaveTypeList.size()==0){
                            return;
                        }
                        leaveTypeOptions.dismiss();
                    }
                });
            }
        }).setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
            @Override
            public void onOptionsSelectChanged(int options1, int options2, int options3) {
                if(leaveTypeList.size()>0){
                    checkedLeaveType = leaveTypeList.get(options1);
                    isChanged = true;
                }
            }
        })
                .setLineSpacingMultiplier(2.4f)
                .setItemVisibleCount(5)
                .setContentTextSize(17)
                .isRestoreItem(true)
                .isAlphaGradient(true)
                .build();
        leaveTypeOptions.setPicker(leaveTypeList);
        leaveTypeOptions.show();
    }
    //加载假期类型数据
    public void initLeaveTypeData(){
        leaveTypeList.add("事假");
        leaveTypeList.add("病假");
        leaveTypeList.add("调休假");
        leaveTypeList.add("婚假");
        leaveTypeList.add("丧假");
    }
//    我已知
    public void iKnowData(){
        iKnowList.add("请假手续不可后补。无特殊情况以旷工计。");
    }

    public void showIKnowPicker(){
        isChanged = false;
        iKnowOptions = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //
                checkedIKnow = iKnowList.get(options1);
            }
        }).setLayoutRes(R.layout.custom_i_know, new CustomListener() {
            @Override
            public void customLayout(View v) {
                final TextView tvSubmit = v.findViewById(R.id.picker_job_inc_bg).findViewById(R.id.include_picker_tv_submit);
                final TextView tvCancle = v.findViewById(R.id.picker_job_inc_bg).findViewById(R.id.include_picker_tv_cancle);
                tvSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(iKnowList.size()==0){
                            return;
                        }
                        if(!isChanged){//没有改变默认选择第一项
                            EventBus.getDefault().post(new PickerMessageEvent(MESSAGE_I_KNOW,iKnowList.get(0),"0"));
                        }else {
                            EventBus.getDefault().post(new PickerMessageEvent(MESSAGE_I_KNOW,checkedIKnow,"0"));
                        }
                        iKnowOptions.dismiss();
                    }
                });
                tvCancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(iKnowList.size()==0){
                            return;
                        }
                        iKnowOptions.dismiss();
                    }
                });
            }
        }).setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
            @Override
            public void onOptionsSelectChanged(int options1, int options2, int options3) {
                if(iKnowList.size()>0){
                    checkedIKnow = iKnowList.get(options1);
                    isChanged = true;
                }
            }
        })
                .setLineSpacingMultiplier(2.4f)
                .setItemVisibleCount(5)
                .setContentTextSize(17)
                .isRestoreItem(true)
                .isAlphaGradient(true)
                .build();
        iKnowOptions.setPicker(iKnowList);
        iKnowOptions.show();
    }
    //需知
    public void needKnowData(){
        needKnowList.add("我已知调休假不可连休，超过三天按事假处理。");
    }

    public void showNeedKnowPricker(){
        isChanged = false;
        needKnowOptions = new OptionsPickerBuilder(context, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //
                checkedNeedKnow = needKnowList.get(options1);
            }
        }).setLayoutRes(R.layout.custom_need_know, new CustomListener() {
            @Override
            public void customLayout(View v) {
                final TextView tvSubmit = v.findViewById(R.id.picker_job_inc_bg).findViewById(R.id.include_picker_tv_submit);
                final TextView tvCancle = v.findViewById(R.id.picker_job_inc_bg).findViewById(R.id.include_picker_tv_cancle);
                tvSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(needKnowList.size()==0){
                            return;
                        }
                        if(!isChanged){//没有改变默认选择第一项
                            EventBus.getDefault().post(new PickerMessageEvent(MESSAGE_NEED_KNOW,needKnowList.get(0),"0"));
                        }else {
                            EventBus.getDefault().post(new PickerMessageEvent(MESSAGE_NEED_KNOW,checkedNeedKnow,"0"));
                        }
                        needKnowOptions.dismiss();
                    }
                });
                tvCancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(needKnowList.size()==0){
                            return;
                        }
                        needKnowOptions.dismiss();
                    }
                });
            }
        }).setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
            @Override
            public void onOptionsSelectChanged(int options1, int options2, int options3) {
                if(needKnowList.size()>0){
                    checkedNeedKnow = needKnowList.get(options1);
                    isChanged = true;
                }
            }
        })
                .setLineSpacingMultiplier(2.4f)
                .setItemVisibleCount(5)
                .setContentTextSize(17)
                .isRestoreItem(true)
                .isAlphaGradient(true)
                .build();

        needKnowOptions.setPicker(needKnowList);
        needKnowOptions.show();
    }


}
